/*
 * Copyright (C) 2012 Jakub Narloch (jmnarloch@gmail.com), All Rights Reserved.
 */

function selectLink(target) {

    removeLinksSelection();

    jQuery(target).addClass("selectedItem");
}

function removeLinksSelection() {

    jQuery("a.selectedItem").removeClass("selectedItem");
}

function retrieveData(url, success) {

    jQuery.getJSON(url, success).error(defaultAjaxErrorHandler);
}

function getUserServices(userId, target) {

    retrieveData("Service/User/" + userId, function(data) {

        var rows = []
        jQuery.each(data, function (index, element) {

            rows.push("<tr><td>" + element.name + "</td></tr>");
        });

        jQuery(target).empty();
        jQuery("<table/>", { html: rows.join("")}).appendTo(target);
    });
}

function getDataSources(userId, target) {

    retrieveData("DataSource/User/" + userId, function(data) {

        var rows = []
        jQuery.each(data, function (index, element) {

            rows.push("<tr><td>" + element.name + "</td></tr>");
        });

        jQuery(target).empty();
        jQuery("<table/>", { html: rows.join("")}).appendTo(target);
    });
}

function getKeyStores(userId, target) {

    retrieveData("KeyStore/User/" + userId, function(data) {

        var rows = []
        jQuery.each(data, function (index, element) {

            rows.push("<tr><td>" + element.name + "</td></tr>");
        });

        jQuery(target).empty();
        jQuery("<table/>", { html: rows.join("")}).appendTo(target);
    });
}

function getServiceTypes(target) {

    jQuery(target).empty();
    retrieveData("Lookup/ServiceType", function(data) {

        jQuery.each(data, function(index, element) {
            jQuery(target)
                .append(jQuery("<option/>", { value : element.id })
                .text(element.name));
        });
    });
}